package com.cat.twoPointerTechnique;

import java.util.Arrays;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/smallest-difference-lcci/
 * @create 2025/8/6 18:44
 * @since JDK17
 */

public class Solution16 {
    public int smallestDifference(int[] a, int[] b) {
        int ans = Integer.MAX_VALUE, n = a.length, m = b.length;
        Arrays.sort(a); // 排升序
        Arrays.sort(b);
        for (int i = 0, j = 0; i < n && j < m; ) {
            ans = Math.min(ans, Math.abs(a[i] - b[j]));
            if (a[i] > b[j]) {
                i++;
            } else if (a[i] < b[j]){
                j++;
            } else {
                return 0;
            }
        }
        return ans;
    }
}
